// ANES24Economics
log using ANES24Economics.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2024_stata_20250219.dta" // ANES 2024 Time Series Study // Preliminary Release: Pre-Election Data February 19, 2025 version

// Create social justice scale
* Rename variables 
rename V241290x Edi 
rename V241372x Tgbathroom 
rename V241375x Tgsport 
rename V241412x Appprotestgaza 

* Delete missing values
foreach var in Edi Tgbathroom Tgsport Appprotestgaza {
    replace `var' = . if `var' < 0
    tabulate `var', missing
}

*Reverse coding so social justice values are high
foreach var in Appprotestgaza Tgbathroom Edi {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rEdi rTgbathroom Tgsport rAppprotestgaza {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this point, the scale has a Cronbach's alpha of 0.76. 

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srEdi srTgbathroom sTgsport srAppprotestgaza {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srEdi srTgbathroom sTgsport srAppprotestgaza  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Economic variables
rename V241294x econbetorworse  
rename V241300x unempbetorworse 
rename V241451 persbetoff 
rename V241476 lookingwork 
rename V241491 outofwork
rename V241492 worrylosejob
rename V241493 reductionhours
rename V241494 jobsat
rename V241539 financialworry
rename V241540 housingpay
rename V241541 anylostjobs
rename V241566x income


// Delete missing values
foreach var in econbetorworse unempbetorworse persbetoff lookingwork outofwork worrylosejob reductionhours jobsat financialworry housingpay anylostjobs income {
    replace `var' = . if `var' < 0
    tabulate `var', missing
}

// Reverse coding so economic precarity is coded low
foreach var in econbetorworse unempbetorworse persbetoff worrylosejob jobsat housingpay {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' - `var'
}

// Correlations
pwcorr SocJusValues reconbetorworse runempbetorworse rpersbetoff lookingwork outofwork rworrylosejob reductionhours rjobsat financialworry rhousingpay anylostjobs income [aweight=V240105a], sig

log close

